package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.SystemDictionary;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ISystemDictionaryService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    //@RequiredPermission(name="部门列表")
    @RequiresPermissions(value={"systemDictionary:list","字典目录列表"},logical= Logical.OR)
    // qo 要贴注解，@ModelAttribute 将数据添加到模型对象中，等价于 model.addAttribute("attributeName", qo);
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo result = systemDictionaryService.query(qo);
        model.addAttribute("result", result);
        return "systemDictionary/list"; // /WEB-INF/views/systemDictionary/list3.ftl
    }

 /*   @RequestMapping("/input")
    @RequiredPermission(name="部门编辑页面")
    public String input(Long id, Model model){
        if(id != null){ // 点击了修改按钮过来
            SystemDictionary systemDictionary = systemDictionaryService.get(id);
            model.addAttribute("systemDictionary", systemDictionary);
        }
        return "/WEB-INF/views/systemDictionary/input.jsp"; // /WEB-INF/views/systemDictionary/input.jsp
    }*/


    @RequestMapping("/saveOrUpdate")
    @ResponseBody   // 告诉 Spring MVC，我们需要转为 json格式返回给前端
   // @RequiredPermission(name="部门保存或修改")
    @RequiresPermissions(value={"systemDictionary:saveOrUpdate","字典目录保存或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary){
        try {
            // int i = 1/0;
            if(systemDictionary.getId() == null){ // 代表新增
                systemDictionaryService.save(systemDictionary);
            }else { // 代表修改
                systemDictionaryService.update(systemDictionary);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
        //return "redirect:/systemDictionary/list.do";  // http://localhost/systemDictionary/list.do
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"systemDictionary:delete","字典目录删除"},logical= Logical.OR)
    public JsonResult delete(Long id){
        // int i = 1/0;
        try {
            if (id != null) {
                systemDictionaryService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
        // return "redirect:/systemDictionary/list.do";  // http://localhost/systemDictionary/list.do
    }
}
